﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.Collections.ObjectModel;
namespace P_PersonalDetails
{
    public partial class PartnerAutoComplete : UserControl
    {
        public PartnerAutoComplete()
        {
            InitializeComponent();
            Box.ItemsSource = PotentialPartners;
        }
        static ObservableCollection<string> PotentialPartners = new ObservableCollection<string>();
        static Service.SlalomRanking.PotentialPartner[] PPs;
        static PartnerAutoComplete()
        {
            try
            {
                var SRV = ServiceConfig.NewSlalomRankingClient();

                SRV.GetAllPotentialPartnersCompleted += (s, e) =>
                    {

                        foreach (var item in e.Result.OrderBy((I) => I.FullName))
                        {
                            PotentialPartners.Add(item.FullName);
                        }
                        PPs = e.Result.ToArray();
                    };
                SRV.GetAllPotentialPartnersAsync();
            }
            catch (Exception)
            {
            }

        }



        public int? SelectedID
        {
            get { return (int?)GetValue(SelectedIDProperty); }
            set { SetValue(SelectedIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedIDProperty =
            DependencyProperty.Register("SelectedID", typeof(int?), typeof(PartnerAutoComplete), new PropertyMetadata(null));



        public string SelectedName
        {
            get { return (string)GetValue(SelectedNameProperty); }
            set { SetValue(SelectedNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedNameProperty =
            DependencyProperty.Register("SelectedName", typeof(string), typeof(PartnerAutoComplete), new PropertyMetadata(""));

        private void Box_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                var Name = e.AddedItems.OfType<string>().First();
                var IDs = PPs.Where((PP) => PP.FullName == Name).Select((PP) => PP.ID);
                if (IDs.Count() == 1)
                    SelectedID = IDs.First();
                else
                    SelectedID = null;
            }
            else
            {
                SelectedID = null;
            }
        }

        
    }
}
